﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;
using MySql.Data.MySqlClient;

namespace Zadani09
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {

        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            Exception vyj = Server.GetLastError();
            HttpContext kontext = HttpContext.Current;

              // zjisteni "nejvnitrnejsi" chyby
            Exception puvodce = vyj;
            while (puvodce.InnerException != null) puvodce = puvodce.InnerException;


              // doslo k mysql chybe?
              // XXX - nestaci pouze vyj.InnerException is MySqlException? 
            Exception mexc = vyj;
            do
            {
                  // jedna ve strukture
                if (mexc is MySqlException)
                {
                    kontext.Items["popis"] = "Problém při práci s databází!";
                    kontext.Items["stacktrace"] = vyj.StackTrace;

                    Server.Transfer("Chyba.aspx");
                }

                mexc = mexc.InnerException;
            } while (mexc != null);


              // neni implementovano, ta bude puvodce vyjimky
            if (puvodce is NotImplementedException)
            {
                kontext.Items["popis"] = "Neni implementovano !";
                kontext.Items["stacktrace"] = vyj.StackTrace;

                Server.Transfer("Chyba.aspx");
            }

            //univerzalni vyjimka zatim..
            //XXX if (vyj is ApplicationException)
            //{
            //    kontext.Items["popis"] = vyj.Message;
            //    kontext.Items["stacktrace"] = vyj.StackTrace;

            //    Server.Transfer("Chyba.aspx");
            //}
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}